package io.chenglicun.modules.point.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.CrudServiceImpl;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.modules.point.dao.PointStandardDao;
import io.chenglicun.modules.point.dto.PointStandardDTO;
import io.chenglicun.modules.point.entity.PointStandardEntity;
import io.chenglicun.modules.point.service.PointStandardService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 指标管理
 *
 * @author gr 137664951@qq.com
 * @since 1.0 2023-09-04
 */
@Service
public class PointStandardServiceImpl extends CrudServiceImpl<PointStandardDao, PointStandardEntity, PointStandardDTO> implements PointStandardService {

    @Override
    public QueryWrapper<PointStandardEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<PointStandardEntity> wrapper = new QueryWrapper<>();
        if(params.containsKey("ROOT")){
            wrapper.lambda().isNull(PointStandardEntity::getParentId);
        }
        wrapper.orderByDesc("create_date","sort");
        return wrapper;
    }


    @Override
    public PageData<PointStandardDTO> pagePointStandard(Map<String, Object> params) {
        params.put("ROOT","YES");
        PageData<PointStandardDTO> page = page(params);
        if(CollectionUtil.isNotEmpty(page.getList())){
            Map<Long,List<PointStandardDTO>> pointStandardMap = mapChildrenPointStandard();
            for(PointStandardDTO psd:page.getList()){
                if(pointStandardMap.containsKey(psd.getId())){

                    psd.setChildren(pointStandardMap.get(psd.getId()));
                }
            }
        }
        return page;
    }

    public Map<Long,List<PointStandardDTO>> mapChildrenPointStandard(){
        List<PointStandardDTO> pointStandards = list( new HashMap<>());
        Map<Long,List<PointStandardDTO>> pointStandardMap = pointStandards.stream().filter(xx->xx.getParentId()!=null)
                .collect(Collectors.groupingBy(PointStandardDTO::getParentId));

        return pointStandardMap;
    }

    @Override
    public List<PointStandardDTO> listPointStandard(){

        List<PointStandardDTO> pointStandards = list( new HashMap<>());
        List<PointStandardDTO> pointStandardDTOS =
                pointStandards.stream().filter(xx->xx.getParentId()==null)
                        .sorted(Comparator.comparing(PointStandardDTO::getSort))
                        .collect(Collectors.toList());
        Map<Long,List<PointStandardDTO>> pointStandardMap = pointStandards.stream().filter(xx->xx.getParentId()!=null)
                        .collect(Collectors.groupingBy(PointStandardDTO::getParentId));
        for (PointStandardDTO standard:pointStandardDTOS){
            standard.setChildren(pointStandardMap.get(standard.getId()));
        }
        return pointStandardDTOS;
    }

    @Override
    public Integer getMaxSort() {
        return baseDao.getMaxSort()+1;
    }
}
